
package com.tianbo.elfii.client.common;

import java.io.*;
import java.sql.* ;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;

/**  <pre>
     <b>类功能描述：</b><pre>
     项目选择基础部分
     </pre>
     @version 1.0.1
     @author 高伟 
*/

public class ElfiiCommonBasicGameSelect  {

    String headline="训练选择";    

    JButton gameAButton;
    JButton gameBButton;
    JButton gameCButton;
    JButton gameDButton;
    JButton gameEButton;
    JButton gameFButton;
    JButton gameGButton;
    JButton gameHButton;
    JButton gameIButton;
    JButton gameJButton;
    JButton gameKButton;
    JButton gameLButton;
    JButton gameMButton;
    JButton gameNButton;

    JButton[] gameButtons;
    String[] gameButtonNames;

    JButton okButton;
    JButton backButton;

    int selectedGameIndex=0;

    JFrame mainFrame;
    ElfiiCommonRunInfo runInfo;
    public void setRunInfo(ElfiiCommonRunInfo runInfo){
        this.runInfo=runInfo;
        this.mainFrame=runInfo.getMainFrame();
        ElfiiCommonConfigUtil.isNoNetPop = true;
    }

    public void dealBackButton(){
        mainFrame.getContentPane().removeAll();
        ElfiiCommonLogin login = new ElfiiCommonLogin();
        login.setRunInfo(runInfo);
        JPanel p=login.createViewPanel();
        mainFrame.getContentPane().setLayout(null);
        mainFrame.getContentPane().add(p);
        p.setBounds(0,0,1024,768);

        mainFrame.validate();
    }

    public JPanel createLevelSelectPanel(){

        okButton=new JButton();
        okButton.addActionListener( new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    dealOkButton();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });
        backButton=new JButton();
        backButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealBackButton();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        gameAButton=new JButton();
        gameAButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(0);
                }
            });
        gameBButton=new JButton();
        gameBButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(1);
                }
            });
        gameCButton=new JButton();
        gameCButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(2);
                }
            });
        gameDButton=new JButton();
        gameDButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(3);
                }
            });
        gameEButton=new JButton();
        gameEButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(4);
                }
            });

        gameFButton=new JButton();
        gameFButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(5);
                }
            });
        gameGButton=new JButton();
        gameGButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(6);
                }
            });
        gameHButton=new JButton();
        gameHButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(7);
                }
            });
        gameIButton=new JButton();
        gameIButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(8);
                }
            });
        gameJButton=new JButton();
        gameJButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(9);
                }
            });
        gameKButton=new JButton();
        gameKButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(10);
                }
            });
        gameLButton=new JButton();
        gameLButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(11);
                }
            });
        gameMButton=new JButton();
        gameMButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(12);
                }
            });
        gameNButton=new JButton();
        gameNButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealGameButton(13);
                }
            });
        gameButtons=new JButton[14];
        gameButtons[0] =gameAButton;
        gameButtons[1] =gameBButton;
        gameButtons[2] =gameCButton;
        gameButtons[3] =gameDButton;
        gameButtons[4] =gameEButton;
        gameButtons[5] =gameFButton;
        gameButtons[6] =gameGButton;
        gameButtons[7] =gameHButton;
        gameButtons[8] =gameIButton;
        gameButtons[9] =gameJButton;
        gameButtons[10]=gameKButton;
        gameButtons[11]=gameLButton;
        gameButtons[12]=gameMButton;
        gameButtons[13]=gameNButton;

        gameButtonNames=new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N"};

        JPanel selectPanel=new JPanel();
        selectPanel.setLayout(null);
        selectPanel.setBackground(Color.black);
        for (int i=0; i<gameButtons.length; i++){
            ImageIcon gameNoSelectedIcon = new ImageIcon("resource-common/images/game choice/"+gameButtonNames[i]+".png","");
            ImageIcon gameSelectedIcon   = new ImageIcon("resource-common/images/game choice/"+gameButtonNames[i]+"_choose.png","");
            gameButtons[i].setBackground(Color.black);
            gameButtons[i].setBorderPainted(false);
            gameButtons[i].setFocusPainted(false);
            gameButtons[i].setIcon(gameNoSelectedIcon);
            gameButtons[i].setSelectedIcon(gameSelectedIcon);
            gameButtons[i].setModel(new DefaultButtonModel() {
                    public boolean isPressed(){
                        return false; 
                    }
                }
                ) ;
            selectPanel.add(gameButtons[i]);
            if (i<7)
                gameButtons[i].setBounds(23+i*140,120,140,130);
            else
                gameButtons[i].setBounds(23+(i-7)*140,300,140,130);
        }
        gameButtons[selectedGameIndex].setSelected(true);
        return selectPanel;
    }

    public void setLastSelectedGameName(String gameName){
        selectedGameIndex=-1;
        gameName=gameName.trim().toLowerCase();
        if (selectedGameIndex==-1 && gameName.endsWith("a"))
            selectedGameIndex=0;
        if (selectedGameIndex==-1 && gameName.endsWith("b"))
            selectedGameIndex=1;
        if (selectedGameIndex==-1 && gameName.endsWith("c"))
            selectedGameIndex=2;
        if (selectedGameIndex==-1 && gameName.endsWith("d"))
            selectedGameIndex=3;
        if (selectedGameIndex==-1 && gameName.endsWith("e"))
            selectedGameIndex=4;
        if (selectedGameIndex==-1 && gameName.endsWith("f"))
            selectedGameIndex=5;
        if (selectedGameIndex==-1 && gameName.endsWith("g"))
            selectedGameIndex=6;
        if (selectedGameIndex==-1 && gameName.endsWith("h"))
            selectedGameIndex=7;
        if (selectedGameIndex==-1 && gameName.endsWith("i"))
            selectedGameIndex=8;
        if (selectedGameIndex==-1 && gameName.endsWith("j"))
            selectedGameIndex=9;
        if (selectedGameIndex==-1 && gameName.endsWith("k"))
            selectedGameIndex=10;
        if (selectedGameIndex==-1 && gameName.endsWith("l"))
            selectedGameIndex=11;
        if (selectedGameIndex==-1 && gameName.endsWith("m"))
            selectedGameIndex=12;
        if (selectedGameIndex==-1 && gameName.endsWith("n"))
            selectedGameIndex=13;
        if (selectedGameIndex==-1)
            selectedGameIndex=0;
    }

    public void dealGameButton(int index)  {
        for (int i=0; i<gameButtons.length; i++){
            if (i!=index)
                gameButtons[i].setSelected(false);
        }
        if (gameButtons[index].isSelected()==false){
            gameButtons[index].setSelected(true);
            ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
        }
    }

    public JPanel createViewPanel(){
        ElfiiCommonRunInfo.menuSlideButtonY=0;
        ElfiiCommonRunInfo.menuLabelStartY=0;

        createLevelSelectPanel();
        JPanel p = new JPanel(new BorderLayout()) ;
        JButton homeButton=runInfo.createHomeButton();
        JPanel topPanel=ElfiiCommonUtil.createTopPanel(headline,backButton,homeButton);
        JPanel bottomPanel=ElfiiCommonUtil.createBottomPanelLevelSelect(okButton);
        p.setLayout(null);
        p.setBackground(Color.black);
        p.add(topPanel);
        topPanel.setBounds(0,0,1024,100);
        p.add(bottomPanel);
        bottomPanel.setBounds(0,668,1024,100);
        JPanel selectPanel=createLevelSelectPanel() ;
        selectPanel.setBackground(Color.black);
        p.add(selectPanel);
        selectPanel.setBounds(0,110,1024,537);
        return p;
    }

    public void dealOkButton(){
        System.out.println("ElfiiCommonBasicGameSelect.dealOkButton() 方法需要被重载");
    }

}
